package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.ListBean;
import com.shd.edanpei.bean.NeedBean;
import com.shd.edanpei.bean.ProductBean;
import com.shd.edanpei.presenter.contract.ProductDetailsContract;
import com.shd.edanpei.enums.OrderTypeEnum;
import com.shd.edanpei.presenter.ProductDetailsPresenter;
import com.shd.edanpei.util.GsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/27.
 * 产品详情
 */

public class ProductDetailsActivity extends BaseActivity<ProductDetailsPresenter>
        implements ProductDetailsContract.ProductDetailsIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_type)
    ImageView ivType;
    @BindView(R.id.tv_headline_right)
    TextView right;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.tv_maxAmount)
    TextView tvMaxAmount;
    @BindView(R.id.et_auditTime)
    EditText etAuditTime;
    @BindView(R.id.tv_rate)
    TextView tvRate;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.tv_carDistance)
    TextView tvCarDistance;
    @BindView(R.id.tv_carAge)
    TextView tvCarAge;
    @BindView(R.id.et_houseSize)
    EditText etHouseSize;
    @BindView(R.id.tv_houseAge)
    TextView tvHouseAge;
    @BindView(R.id.ll_car)
    View car;
    @BindView(R.id.ll_house)
    View house;
    //    @BindView(R.id.et_repayWay)
//    EditText etRepayWay;
    @BindView(R.id.tv_repayWay)
    TextView tvRepayWay;
    @BindView(R.id.tv_repayDate)
    TextView tvRepayDate;
    @BindView(R.id.tv_needData)
    TextView tvNeedData;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.iv_next)
    View next;
    @BindView(R.id.iv_next1)
    View next1;
    @BindView(R.id.iv_next2)
    View next2;
    @BindView(R.id.iv_next3)
    View next3;
    @BindView(R.id.iv_next4)
    View next4;
    @BindView(R.id.iv_next5)
    View next5;
    @BindView(R.id.iv_next6)
    View next6;
    //    @BindView(R.id.iv_next7)
//    View next7;
    @BindView(R.id.iv_next8)
    View next8;
    @BindView(R.id.iv_next9)
    View next9;
    @BindView(R.id.iv_next10)
    View next10;

    private String id;
    private int index;
    private ProductBean bean;
    private boolean isEdit = false;
    private List<NeedBean> needBeanList = new ArrayList<>();

    @Override
    protected int setLayout() {
        return R.layout.activity_product;
    }

    @Override
    protected ProductDetailsPresenter bindPresenter() {
        return new ProductDetailsPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("产品详情");
        Bundle extras = getIntent().getExtras();
        id = extras.getString(Constant.ID);
        index = extras.getInt(Constant.INDEX);
        mPresenter.getData(id);
        tvSubmit.setVisibility(View.GONE);
    }

    @Override
    protected void initListener() {
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEdit) {
                    right.setText("编辑");
                    //点击了取消未保存，恢复原数据
                    mPresenter.clickCancel();
                } else {
                    right.setText("取消");
                }
                isEdit = !isEdit;
                setData(bean);
            }
        });
        etAuditTime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!isEdit) return;
                mPresenter.setAuditTime(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etHouseSize.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!isEdit) return;
                mPresenter.setHouseSize(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
//        etRepayWay.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if (!isEdit) return;
//                mPresenter.setRepayWay(s.toString());
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }


    @OnClick(R.id.ll_maxAmount)
    void onMaxAmount() {
        if (!isEdit) return;
        mPresenter.setMaxAmount(tvMaxAmount);
    }


    @OnClick(R.id.ll_rate)
    void onRate() {
        if (!isEdit) return;
        mPresenter.setRate(tvRate);
    }


    @OnClick(R.id.ll_age)
    void onAge() {
        if (!isEdit) return;
        mPresenter.setAge(tvAge);
    }


    @OnClick(R.id.ll_carDistance)
    void onCarDisance() {
        if (!isEdit) return;
        mPresenter.setCarDistance(tvCarDistance);
    }

    @OnClick(R.id.ll_carAge)
    void onCarAge() {
        if (!isEdit) return;
        mPresenter.setCarAge(tvCarAge);
    }

    @OnClick(R.id.ll_houseAge)
    void onHouseAge() {
        if (!isEdit) return;
        mPresenter.setHouseAge(tvHouseAge);
    }

    @OnClick(R.id.ll_repayDate)
    void onRepayDate() {
        if (!isEdit) return;
        mPresenter.setRepayDate(tvRepayDate);
    }

    @OnClick(R.id.rl_needData)
    void onNeedData() {
        if (!isEdit) return;
        Bundle bundle = new Bundle();
        bundle.putString(Constant.TITLE, "所需材料");
        bundle.putInt(Constant.TAG, Constant.NEED_DATA);
        bundle.putString(Constant.DATA, GsonUtil.toJson(needBeanList));
        startActivity(SelectActivity.class, bundle);
    }

    @OnClick(R.id.tv_submit)
    void onSubmit() {
        mPresenter.submit(id, index);
    }

    @Subscribe(code = SelectActivity.DONE_SELECT)
    public void doneSelect(ListBean listBean) {
        String data = "";
        for (NeedBean needBean : listBean.getNeedBeanList()) {
            if (needBean.isCheck()) {
                if (data.isEmpty()) {
                    data = needBean.getName();
                } else {
                    data = data + ";" + needBean.getName();
                }
            }
        }
        switch (listBean.getType()) {
            case Constant.NEED_DATA:
                needBeanList = listBean.getNeedBeanList();
                setText(tvNeedData, data);
                mPresenter.setNeedData(data);
                break;
        }


    }

    @Override
    public void setData(ProductBean bean) {
        if (bean == null) {
            return;
        }
        this.bean = bean;
        tvType.setText(bean.getProduct());
        ivType.setImageDrawable(getResources().getDrawable((bean.getProductType() == Constant.CAR) ? R.drawable.ic_label_car : R.drawable.ic_label_house));

        if (isEdit) {
            showView(next, next1, next2, next3, next4, next5, next6, next8, next9, next10);
            setEditText(true, etAuditTime, etHouseSize);
        } else {
            hideView(next, next1, next2, next3, next4, next5, next6, next8, next9, next10);
            setEditText(false, etAuditTime, etHouseSize);
        }
        setText(tvArea, bean.getAreaDesc());
        setText(tvMaxAmount, bean.getLoanMax());
        setText(etAuditTime, bean.getLoanDay());
        setText(tvRate,  bean.getRateMax() + Constant.Unit.LI.getUnit());
        setText(tvAge, bean.getAgeMin() + "—" + bean.getAgeMax() + Constant.Unit.AGE.getUnit());

        if (bean.getProductType() == OrderTypeEnum.car.getKey()) {
            car.setVisibility(View.VISIBLE);
            setText(tvCarDistance, bean.getTravelWkm());
            setText(tvCarAge, bean.getCartYear());
        } else {
            house.setVisibility(View.VISIBLE);
            setText(etHouseSize, bean.getRoomSize());
            setText(tvHouseAge, bean.getRoomAge());
        }
        setText(tvRepayWay, bean.getRepayMode());
        setText(tvRepayDate, bean.getRepayTerm());
        setText(tvNeedData, bean.getLoanData());
        tvSubmit.setText(bean.getButtonStatus());
        //是否可以编辑
        if (bean.isUp()) {
            right.setVisibility(View.GONE);
        } else {
            right.setVisibility(View.VISIBLE);
            right.setText("编辑");
        }

    }


    @Override
    public void setText(TextView text, String str) {
        text.setText(str);
        text.setTextColor(ContextCompat.getColor(this, R.color.black));
    }

    private void showView(View... views) {
        for (View view : views) {
            view.setVisibility(View.VISIBLE);
        }
    }

    private void hideView(View... views) {
        for (View view : views) {
            view.setVisibility(View.GONE);
        }
    }

    private void setEditText(boolean b, EditText... editTexts) {
        for (EditText editText : editTexts) {
            editText.setFocusable(b);
            editText.setFocusableInTouchMode(b);
        }
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }
}
